package com.xuesaieducation.zshddemo.fragments;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.gensee.media.VODPlayer;
import com.gensee.pdu.GSDocView;
import com.gensee.player.Player;
import com.gensee.view.GSDocViewEx;
import com.gensee.view.GSDocViewGx;
import com.xuesaieducation.zshddemo.R;
import com.xuesaieducation.zshddemo.ScreenUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class DocFragment extends Fragment
{

    @BindView(R.id.imGlDocView)
    GSDocViewGx mGlDocView;
    @BindView(R.id.wait)
    ProgressBar wait;
    @BindView(R.id.full_screen)
    ImageView fullScreen;
    @BindView(R.id.layout)
    FrameLayout parent;
    Unbinder unbinder;

    private Player mPlayer;
    private VODPlayer mVodPlayer;

    private Activity mContext;

    Animation rightbarAppearAnim, rightbarDisappearAnim;

    public void setLivePlayer(Player player)
    {
        this.mPlayer = player;
    }

    public void setVodPlayer(VODPlayer player)
    {
        this.mVodPlayer = player;
    }

    @Override
    public void onAttach(Context context)
    {
        super.onAttach(context);
        if (context instanceof Activity)
            mContext = (Activity) context;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        return inflater.inflate(R.layout.imdoc, null);
    }

    int parentPortraitWidth, parentPortraitHeight;
    boolean isViewCreated, isViewDestroyed;

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState)
    {
        super.onViewCreated(view, savedInstanceState);
        unbinder = ButterKnife.bind(this, view);
        if (mPlayer != null)
        {
            mPlayer.setGSDocViewGx(mGlDocView);
        } else if (mVodPlayer != null)
        {
            mVodPlayer.setGSDocViewGx(mGlDocView);
        }
        parent.post(new Runnable()
        {
            @Override
            public void run()
            {
                parentPortraitWidth = parent.getMeasuredWidth();
                parentPortraitHeight = parent.getMeasuredHeight();
                fullScreen.setVisibility(View.VISIBLE);
            }
        });
        mGlDocView.setOnPageOpenListener(new GSDocViewEx.OnPageOpenListener()
        {
            @Override
            public void onPageOpenFileFailure(int i)
            {
                mGlDocView.post(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        if (wait != null)
                            wait.setVisibility(View.GONE);
                    }
                });
            }

            @Override
            public void onPageOpenComplete(int i, int i1)
            {
                mGlDocView.post(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        if (wait != null)
                            wait.setVisibility(View.GONE);
                    }
                });
            }
        });

        wait.setVisibility(View.VISIBLE);

        isViewCreated = true;
    }

    public void reBindPlayer(Player p)
    {
        mPlayer = p;
        mPlayer.setGSDocViewGx(mGlDocView);
    }

    public void reBindPlayer(VODPlayer p)
    {
        mVodPlayer = p;
        mVodPlayer.setGSDocViewGx(mGlDocView);
    }

    @Override
    public void onDestroyView()
    {
        super.onDestroyView();
        unbinder.unbind();
        if (mGlDocView != null)
        {
            mGlDocView.closeDoc();
        }
        isViewDestroyed = true;
    }

    @OnClick(value = {R.id.full_screen})
    public void onViewClicked(View v)
    {
        switch (v.getId())
        {
            case R.id.full_screen:
                if (ll != null)
                    ll.docClick();
                break;
        }
    }

    public void handleScreenOritention()
    {
        if (ScreenUtils.isPortrait(mContext))
        {
            fullScreen.setImageResource(R.drawable.full_screen);
            ViewPager.LayoutParams p = (ViewPager.LayoutParams) parent.getLayoutParams();
            p.width = parentPortraitWidth;
            p.height = parentPortraitHeight;
            parent.setLayoutParams(p);
        } else
        {
            fullScreen.setImageResource(R.drawable.normal_screen);
            ViewPager.LayoutParams p = (ViewPager.LayoutParams) parent.getLayoutParams();
            p.width = ScreenUtils.screenWidth(mContext);
            p.height = ScreenUtils.screenHeight(mContext);
            parent.setLayoutParams(p);
        }
    }

    public void pauseDoc()
    {
        if (mGlDocView != null)
        {
            mGlDocView.onPause();
            mGlDocView.setVisibility(View.GONE);
        }
    }

    public void resumeDoc()
    {
        if (mGlDocView != null)
        {
            mGlDocView.setVisibility(View.VISIBLE);
            mGlDocView.onResume();
        }
    }

    public interface FullScreenClickListener
    {
        void docClick();
    }

    public void setFullScreenClickListener(FullScreenClickListener cc)
    {
        ll = cc;
    }

    FullScreenClickListener ll;
}
